﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebChatServer.Models;

namespace WebChatServer.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    [HttpHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")]
    public class GroupController : ApiController
    {
        IGroupRepository _repository = new GroupRepository();

        public void Options()
        {
            
        }
        //
        // GET: /FriendGroup/
        

        public FriendGroup Get(string id)
        { 
            return _repository.GetGroup(id);
        }

        public FriendGroup Post(FriendGroup gGroup)
        {
            //return _repository.Add(stu);
            gGroup = _repository.AddGroup(gGroup);
            var response = Request.CreateResponse<FriendGroup>(HttpStatusCode.Created, gGroup);

            string uri = Url.Link("DefaultApi", new { id = gGroup.UserName });
            response.Headers.Location = new Uri(uri);

            return gGroup;
        }

        public HttpResponseMessage Delete(string id)
        {
            _repository.DeleteGroup(id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
